/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PChangePassword.java
 *
 * Created on Mar 26, 2012, 1:26:25 PM
 */
package com.bc5Neptune.cis.gui;

import com.bc5Neptune.cis.config.ConnectDB2;
import com.bc5Neptune.cis.dal.Employee;
import java.sql.*;

public class PChangePassword extends javax.swing.JPanel {

    ConnectDB2 obj = new ConnectDB2();

    /** Creates new form PChangePassword */
    public PChangePassword() {
        initComponents();
        obj.getConnection();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel17 = new javax.swing.JPanel();
        pwfnewpass = new javax.swing.JPasswordField();
        pwfconfirmpass = new javax.swing.JPasswordField();
        pwfcurpass = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnreset = new javax.swing.JButton();
        lblnotice = new javax.swing.JLabel();
        btnOK = new javax.swing.JButton();
        btncancel = new javax.swing.JButton();

        setName("Change Password"); // NOI18N

        jSplitPane1.setDividerLocation(50);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel2.setBackground(java.awt.Color.lightGray);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 729, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        jSplitPane1.setTopComponent(jPanel2);

        jPanel3.setBackground(new java.awt.Color(255, 194, 0));

        jPanel17.setBackground(new java.awt.Color(255, 194, 0));
        jPanel17.setBorder(javax.swing.BorderFactory.createCompoundBorder());

        pwfnewpass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pwfnewpassActionPerformed(evt);
            }
        });

        pwfconfirmpass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pwfconfirmpassActionPerformed(evt);
            }
        });

        pwfcurpass.setVerifyInputWhenFocusTarget(false);
        pwfcurpass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pwfcurpassActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Abyssinica SIL", 3, 15));
        jLabel4.setText("Current password:");

        jLabel5.setFont(new java.awt.Font("Abyssinica SIL", 3, 15));
        jLabel5.setText("New password:");

        jLabel6.setFont(new java.awt.Font("Abyssinica SIL", 3, 15));
        jLabel6.setText("Confirm password:");

        btnreset.setFont(new java.awt.Font("Abyssinica SIL", 1, 15));
        btnreset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/reset2.jpg"))); // NOI18N
        btnreset.setText("Reset");
        btnreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnresetActionPerformed(evt);
            }
        });

        lblnotice.setFont(new java.awt.Font("Abyssinica SIL", 0, 18));
        lblnotice.setForeground(java.awt.Color.red);

        btnOK.setFont(new java.awt.Font("Abyssinica SIL", 1, 15));
        btnOK.setText("Change");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btncancel.setFont(new java.awt.Font("Abyssinica SIL", 1, 15));
        btncancel.setText("Cancel");
        btncancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel17Layout = new javax.swing.GroupLayout(jPanel17);
        jPanel17.setLayout(jPanel17Layout);
        jPanel17Layout.setHorizontalGroup(
            jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel17Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel17Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addContainerGap(547, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel17Layout.createSequentialGroup()
                        .addGap(229, 229, 229)
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(btnreset, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47)
                        .addComponent(btncancel, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29))
                    .addGroup(jPanel17Layout.createSequentialGroup()
                        .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(pwfnewpass, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pwfconfirmpass, javax.swing.GroupLayout.PREFERRED_SIZE, 463, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(jPanel17Layout.createSequentialGroup()
                        .addComponent(lblnotice, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(222, Short.MAX_VALUE))
                    .addGroup(jPanel17Layout.createSequentialGroup()
                        .addComponent(pwfcurpass, javax.swing.GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE)
                        .addGap(230, 230, 230))
                    .addGroup(jPanel17Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(551, Short.MAX_VALUE))
                    .addGroup(jPanel17Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addContainerGap(576, Short.MAX_VALUE))))
        );
        jPanel17Layout.setVerticalGroup(
            jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel17Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pwfcurpass, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pwfnewpass, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pwfconfirmpass, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(lblnotice, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addGroup(jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnreset, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btncancel, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(58, Short.MAX_VALUE)
                .addComponent(jPanel17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39))
        );

        jSplitPane1.setRightComponent(jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 729, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void pwfnewpassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pwfnewpassActionPerformed
// TODO add your handling code here:
    btnOKActionPerformed(evt);
}//GEN-LAST:event_pwfnewpassActionPerformed

private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
// TODO add your handling code here:
    changePassword();

}//GEN-LAST:event_btnOKActionPerformed

    public void changePassword() {

        lblnotice.setText(null);
        String newpass = pwfnewpass.getText();
        String existpass = pwfcurpass.getText();
        String confpass = pwfconfirmpass.getText();
        int i = 0;
        Employee obj = new Employee();
        String dbPass = null;
        if (newpass.equals(confpass) == false) {
            lblnotice.setText("You enter password wrong!");
            pwfconfirmpass.setText(null);
            pwfcurpass.setText(null);
            pwfnewpass.setText(null);
            return;
        }
        try {

            dbPass = obj.selectpass(existpass).getPassword();
            System.out.println(dbPass);
            i = obj.updatepass(newpass, existpass);
            System.out.println(i);
            if (i == -1) {
                lblnotice.setText("Changed password!");
                return;

            } else if (newpass.isEmpty() == true) {
                lblnotice.setText("Please enter new password!");
                return;
            } else if (existpass.isEmpty() == true) {
                lblnotice.setText("Please enter current password!");
                return;
            } else if (confpass.isEmpty() == true) {
                lblnotice.setText("Please enter confirm password!");
                return;
            } else if (existpass.equalsIgnoreCase(dbPass) == false) {
                lblnotice.setText("Password does not exist in database! Please try again!");
                pwfconfirmpass.setText(null);
                pwfcurpass.setText(null);
                pwfnewpass.setText(null);
                return;
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

private void btnresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnresetActionPerformed
// TODO add your handling code here:
    lblnotice.setText(null);
    pwfnewpass.setText(null);
    pwfconfirmpass.setText(null);
    pwfcurpass.setText(null);

}//GEN-LAST:event_btnresetActionPerformed

private void pwfcurpassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pwfcurpassActionPerformed
// TODO add your handling code here:
    btnOKActionPerformed(evt);
}//GEN-LAST:event_pwfcurpassActionPerformed

private void pwfconfirmpassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pwfconfirmpassActionPerformed
// /TODO add your handling code here:
    btnOKActionPerformed(evt);
}//GEN-LAST:event_pwfconfirmpassActionPerformed

private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelActionPerformed
// TODO add your handling code here:
    System.exit(0);
}//GEN-LAST:event_btncancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOK;
    private javax.swing.JButton btncancel;
    private javax.swing.JButton btnreset;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lblnotice;
    private javax.swing.JPasswordField pwfconfirmpass;
    private javax.swing.JPasswordField pwfcurpass;
    private javax.swing.JPasswordField pwfnewpass;
    // End of variables declaration//GEN-END:variables
}
